package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.com.ruanmeng.demon.KFPDListM;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CustomGridView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 新加康复评定列表
 */
public class KangFuPingDingListActivity extends BaseActivity {

    @BindView(R.id.rlist_kfpd)
    RecyclerView rlistKfpd;
    @BindView(R.id.refresh_kfpd)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.view_kfpd_line)
    View viewKfpdLine;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<KFPDListM.RowsBean> Temp_list = new ArrayList<>();
    private JianceListAdapter mAdapter;
    int page = 1;
    private LinearLayout li_kfpd_nuill;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kang_fu_ping_ding_list);
        ButterKnife.bind(this);
        ChangLayTitle("健康档案");
        LayBack();
        ShowLayAdd();
        init();
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Params.CreateKFPD == 1) {
            Params.CreateKFPD = 0;
            page = 1;
            getData();
        }
    }

    public void init() {
        //空
        li_kfpd_nuill = (LinearLayout) findViewById(R.id.li_kfpd_nuill);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistKfpd.setLayoutManager(linearLayoutManager);
        mAdapter = new JianceListAdapter(KangFuPingDingListActivity.this, rlistKfpd, Temp_list, R.layout.item_kfpdlist);
        rlistKfpd.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //是否评估结束  评估过得不能在修改  2只能浏览
                Params.assessStatus = 2;
//                Params.assessStatus=Temp_list.get(position+1).getAssessStatus();
                Params.cureTarget = Temp_list.get(position + 1).getCureTarget();
                Params.isDati = Temp_list.get(position + 1).getIsDati();
                Params.JJZ = Temp_list.get(position + 1).getContrtions();
                Params.MuBiao = Temp_list.get(position + 1).getCureTarget();
                Intent in = new Intent(KangFuPingDingListActivity.this, ChoosePainPlaceActivity.class);
                in.putExtra("id", Temp_list.get(position + 1).getAssessId());
                in.putExtra("name", Temp_list.get(position + 1).getUserName());
                in.putExtra("tz", Temp_list.get(position + 1).getWeight());
                in.putExtra("sg", Temp_list.get(position + 1).getStature());
                in.putExtra("age", Temp_list.get(position + 1).getAge());
//                in.putExtra("mubiao", Temp_list.get(position + 1).getCureTarget());//期望目标
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.KFPDList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(KangFuPingDingListActivity.this, "uid"));
        mRequest.add("page", page);
        // 添加到请求队列
        CallServer.getRequestInstance().add(KangFuPingDingListActivity.this, 0, mRequest,
                new CustomHttpListener(KangFuPingDingListActivity.this, true, KFPDListM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        KFPDListM model = ((KFPDListM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_kfpd_nuill.setVisibility(View.VISIBLE);
                            rlistKfpd.setVisibility(View.GONE);
                        } else {
                            li_kfpd_nuill.setVisibility(View.GONE);
                            rlistKfpd.setVisibility(View.VISIBLE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }


    public class JianceListAdapter extends BaseLoadMoreHeaderAdapter<KFPDListM.RowsBean> {


        private String[] jj_str;

        public JianceListAdapter(Context mContext, RecyclerView recyclerView, List<KFPDListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, final RecyclerView.ViewHolder holder, KFPDListM.RowsBean s) {
            CustomGridView jinji = (CustomGridView) holder.itemView.findViewById(R.id.gv_kfpdlist);
            TextView tv_kfpdlist_jinji = (TextView) holder.itemView.findViewById(R.id.tv_kfpdlist_jinji);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_kfpdlist_name);
            TextView years = (TextView) holder.itemView.findViewById(R.id.tv_kfpdlist_years);
            TextView tv_zixun = (TextView) holder.itemView.findViewById(R.id.tv_zixun);
            TextView tv_pgbg = (TextView) holder.itemView.findViewById(R.id.tv_pgbg);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_kfpdl_time);
            TextView tv_zdy = (TextView) holder.itemView.findViewById(R.id.tv_zdy);
            View view_fif = (View) holder.itemView.findViewById(R.id.view_fif);
            LinearLayout li_man = (LinearLayout) holder.itemView.findViewById(R.id.li_list_man);
            LinearLayout li_jjz = (LinearLayout) holder.itemView.findViewById(R.id.li_item_jjz);
            //设置数据
            name.setText(s.getUserName());
            years.setText(s.getAge() + "");
            tv_zixun.setText("咨询（" + s.getQuestionNum() + "）");
            time.setText(s.getCreateDate());
            //禁忌症
            if (!TextUtils.isEmpty(s.getContrtions())) {
                tv_kfpdlist_jinji.setText(s.getContrtions());
            } else {
                tv_kfpdlist_jinji.setText("无");
            }
//            ArrayList<String> Temp_listjj = new ArrayList<>();
//            String jinjiz_str = s.getContrtions();
//            if (!TextUtils.isEmpty(jinjiz_str)) {
//                jj_str = jinjiz_str.split(",");
//                for (int i = 0; i < jj_str.length; i++) {
//                    Temp_listjj.add(jj_str[i]);
//                }
//
////                li_jjz.setVisibility(View.VISIBLE);
////                view_fif.setVisibility(View.GONE);
//            } else {
//                Temp_listjj.add("无");
////                li_jjz.setVisibility(View.GONE);
////                view_fif.setVisibility(View.INVISIBLE);
//            }
//            JinJiListAdapter jjadapter = new JinJiListAdapter(Temp_listjj);
//            jinji.setAdapter(jjadapter);
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                li_man.setVisibility(View.GONE);
            } else {
                li_man.setVisibility(View.VISIBLE);
            }
            jinji.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Params.cureTarget = Temp_list.get(holder.getLayoutPosition()).getCureTarget();
                    Params.isDati = Temp_list.get(holder.getLayoutPosition()).getIsDati();
                    Params.JJZ = Temp_list.get(holder.getLayoutPosition()).getContrtions();
                    Params.MuBiao = Temp_list.get(holder.getLayoutPosition()).getCureTarget();
                    Intent in = new Intent(KangFuPingDingListActivity.this, ChoosePainPlaceActivity.class);
                    in.putExtra("id", Temp_list.get(holder.getLayoutPosition()).getAssessId());
                    in.putExtra("name", Temp_list.get(holder.getLayoutPosition()).getUserName());
                    in.putExtra("tz", Temp_list.get(holder.getLayoutPosition()).getWeight());
                    in.putExtra("sg", Temp_list.get(holder.getLayoutPosition()).getStature());
                    in.putExtra("age", Temp_list.get(holder.getLayoutPosition()).getAge());
                    startActivity(in);
                }
            });
            tv_pgbg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(KangFuPingDingListActivity.this, ReportActivity.class);
                    in.putExtra("id", Temp_list.get(holder.getLayoutPosition()).getAssessId());
                    startActivity(in);
                }
            });
            tv_zdy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(KangFuPingDingListActivity.this, ZDYReportActivity.class);
                    in.putExtra("id", Temp_list.get(holder.getLayoutPosition()).getAssessId());
                    startActivity(in);
                }
            });

        }
    }

    /**
     * 禁忌症
     */
    class JinJiListAdapter extends BaseAdapter {
        private List<String> list;

        public JinJiListAdapter(List<String> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_jinji, null);
            TextView name = (TextView) v.findViewById(R.id.tv_item_jinjiname);
            ImageView imv_item_choose = (ImageView) v.findViewById(R.id.imv_item_choose);
            imv_item_choose.setVisibility(View.GONE);
            name.setText(list.get(position));
            return v;
        }

    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.tv_lay_add://新建
//                if (!("1").equals(PreferencesUtils.getString(KangFuPingDingListActivity.this, "statues"))) {//强制补充个人资料
//                    ShowPop();
//                } else {
                Intent in = new Intent(KangFuPingDingListActivity.this, KangFuPDActivity.class);
                in.putExtra("new", "1");
                startActivity(in);
//                }
                break;
        }
    }

    /**
     * 强制完善个人资料
     */
    public void ShowPop() {
        // 加载布局
        LayoutInflater inflater = LayoutInflater
                .from(KangFuPingDingListActivity.this);
        View view = inflater.inflate(R.layout.dialog_mustlayout, null);
        TextView tv_must_sure = (TextView) view.findViewById(R.id.tv_must_sure);
        // 创建popupwindow对话框
        final PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);// 必须有，因为要获得焦点进行输入
        // 设置此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 点击其他除了popupwindow对话框的地方就可使popupwindow对话框消失
        pop.setOutsideTouchable(true);
        //pop.showAtLocation(liFr, Gravity.CENTER, 0, 0);// 显示位置以某控件为中心
        if (Build.VERSION.SDK_INT >= 24) {
            int[] a = new int[2];
            viewKfpdLine.getLocationInWindow(a);
            pop.showAtLocation((KangFuPingDingListActivity.this).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + viewKfpdLine.getHeight());
        } else {
            pop.showAsDropDown(viewKfpdLine);
        }
        tv_must_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 请求接口
                pop.dismiss();
                Intent in = new Intent(KangFuPingDingListActivity.this, MustSetPerMessActivity.class);
                startActivity(in);
            }
        });

    }
}

